883H - Palindromic Cut - CodeForces Solution


brute force implementation strings *1800

Please click on ads to support us..

C++ Code:

#include "bits/stdc++.h"
using namespace std;
const int N=4e5+10,M=125;
int mp[M],ans;
vector<char>ss;
int n;string s;
int check(){
    int j=0;
    while(true)
    {
        ans=ss.size();
        int c=(n-ans)>>1;
        if(c%ans==0)//每组多少对.
            return c;
        while(j<M&& !mp[j])
            j++;
        mp[j]--;
        ss.push_back(char(j));
        ss.push_back(char(j));
    }
}
int main(){
    memset(mp,0,sizeof(mp));
    cin>>n>>s;
    for (int i = 0; i <n ; ++i) {
        mp[s[i]]++;
    }
    for(int i=0;i<M;i++)
    {
        if(mp[i]%2)
            ss.push_back(char(i));
        mp[i]/=2;
    }
    if(ss.size()==0)
    {
        cout<<1<<endl;
        string B="";
        for(int j=0;j<M;j++)
            for(int k=0;k<mp[j];k++)
                B+=char(j);
        string C=B;
        reverse(C.begin(),C.end());
        B+=C;
        printf("%s\n",B.c_str());
        return 0;
    }
    int c=check(),j=0;
    cout<<ans<<endl;
    for (int i = 0; i <ans ; ++i) {
        char x=ss[i];
        int d=c/ans;
        string B="";
        while (1){
            while(j<M&& !mp[j]) j++;
            if (mp[j]>=d){
                for(int k=0;k<d;k++)
                    B+=char(j);
                mp[j]-=d,d=0;
                break;
            }
            else {
                for(int k=0;k<mp[j];k++)
                    B+=char(j);
                d-=mp[j],mp[j]=0;
            }
        }
        string C=B;
        reverse(C.begin(),C.end());
        B=B+x+C;
        printf("%s ",B.c_str());
    }
}


Comments

Submit
0 Comments
More Questions

802M - April Fools' Problem (easy)
577B - Modulo Sum
1555B - Two Tables
1686A - Everything Everywhere All But One
1469B - Red and Blue
1257B - Magic Stick
18C - Stripe
1203B - Equal Rectangles
1536A - Omkar and Bad Story
1509A - Average Height
1506C - Double-ended Strings
340A - The Wall
377A - Maze
500A - New Year Transportation
908D - New Year and Arbitrary Arrangement
199A - Hexadecimal's theorem
519C - A and B and Team Training
631A - Interview
961B - Lecture Sleep
522A - Reposts
1166D - Cute Sequences
1176A - Divide it
1527A - And Then There Were K
1618E - Singers' Tour
1560B - Who's Opposite
182B - Vasya's Calendar
934A - A Compatible Pair
1618F - Reverse
1684C - Column Swapping
57C - Array